package logic

import (
	"context"
	"nate_srv/models"

	"nate_srv/internal/svc"
	"nate_srv/nate"

	"github.com/zeromicro/go-zero/core/logx"
)

type NateInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNateInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NateInfoLogic {
	return &NateInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NateInfoLogic) NateInfo(in *nate.NateInfoRequest) (*nate.NateInfoResponse, error) {
	// todo: add your logic here and delete this line

	var ts []models.TAges
	var nt models.NateInfo
	var listNt []*nate.Tags

	models.DB.Table("nates").
		Select("nates.title as title,nates.context as context,users.nick_name user_name,tags.name as tag").
		Joins("LEFT JOIN nates on nates.user_id=users.id").
		Joins("LEFT JOIN tags on tags.nate_id=nates.id").
		Find(&nt)

	for _, t := range ts {
		list := nate.Tags{
			TagsName: t.Name,
		}
		listNt = append(listNt, &list)
	}

	return &nate.NateInfoResponse{
		Title:    nt.Title,
		Context:  nt.Context,
		UserName: nt.UserName,
		List:     listNt,
	}, nil
}
